<template>
  <div class="asset-page">
    <el-card>
      <div style="margin-bottom: 16px; display: flex; justify-content: space-between; align-items: center;">
        <span style="font-weight: bold;">资产档案管理</span>
        <el-button type="primary" size="small" @click="onAdd">新增资产</el-button>
      </div>
      <el-table :data="list" border stripe style="width: 100%;">
        <el-table-column prop="id" label="ID" width="60" />
        <el-table-column prop="name" label="资产名称" min-width="120" />
        <el-table-column prop="categoryId" label="分类" min-width="100">
          <template #default="scope">
            {{ categoryMap[scope.row.categoryId] || '-' }}
          </template>
        </el-table-column>
        <el-table-column prop="brand" label="品牌" min-width="100" />
        <el-table-column prop="model" label="型号" min-width="100" />
        <el-table-column prop="serialNo" label="序列号" min-width="120" />
        <el-table-column prop="location" label="存放地点" min-width="120" />
        <el-table-column prop="status" label="状态" min-width="80">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">{{ scope.row.status === 1 ? '在用' : '闲置' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button size="small" @click="onEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="onDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 新增/编辑弹窗 -->
    <el-dialog v-model="dialog.visible" :title="dialog.isEdit ? '编辑资产' : '新增资产'" width="500px">
      <el-form :model="dialog.form" label-width="100px">
        <el-form-item label="资产名称" required>
          <el-input v-model="dialog.form.name" placeholder="请输入资产名称" />
        </el-form-item>
        <el-form-item label="分类" required>
          <el-select v-model="dialog.form.categoryId" placeholder="请选择分类">
            <el-option v-for="item in categoryList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="品牌">
          <el-input v-model="dialog.form.brand" />
        </el-form-item>
        <el-form-item label="型号">
          <el-input v-model="dialog.form.model" />
        </el-form-item>
        <el-form-item label="序列号">
          <el-input v-model="dialog.form.serialNo" />
        </el-form-item>
        <el-form-item label="存放地点">
          <el-input v-model="dialog.form.location" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="dialog.form.status">
            <el-option :value="1" label="在用" />
            <el-option :value="0" label="闲置" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="dialog.form.remark" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialog.visible = false">取消</el-button>
        <el-button type="primary" @click="doSave">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '@/utils/request'

const list = ref([])
const categoryList = ref([])
const categoryMap = reactive({})
const fetchList = async () => {
  const res = await request.get('/asset/list')
  if (res.code === 1) {
    list.value = res.data
  }
}
const fetchCategoryList = async () => {
  const res = await request.get('/asset/category/list')
  if (res.code === 1) {
    categoryList.value = res.data
    res.data.forEach(item => { categoryMap[item.id] = item.name })
  }
}
const dialog = reactive({ visible: false, isEdit: false, form: { id: null, name: '', categoryId: null, brand: '', model: '', serialNo: '', location: '', status: 1, remark: '' } })
const onAdd = () => {
  dialog.isEdit = false
  dialog.form = { id: null, name: '', categoryId: null, brand: '', model: '', serialNo: '', location: '', status: 1, remark: '' }
  dialog.visible = true
}
const onEdit = (row) => {
  dialog.isEdit = true
  dialog.form = { ...row }
  dialog.visible = true
}
const doSave = async () => {
  if (!dialog.form.name || !dialog.form.categoryId) {
    ElMessage.warning('资产名称和分类不能为空')
    return
  }
  if (dialog.isEdit) {
    await request.put('/asset', dialog.form)
    ElMessage.success('修改成功')
  } else {
    await request.post('/asset', dialog.form)
    ElMessage.success('新增成功')
  }
  dialog.visible = false
  fetchList()
}
const onDelete = (row) => {
  ElMessageBox.confirm('确认删除该资产？', '提示', { type: 'warning' }).then(async () => {
    await request.delete(`/asset/${row.id}`)
    ElMessage.success('删除成功')
    fetchList()
  })
}
onMounted(() => {
  fetchCategoryList()
  fetchList()
})
</script>

<style scoped>
.asset-page {
  padding: 24px;
}
</style> 